package com.hengxinguotong.zhihuichengjian.widget.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Structure;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/1.
 */

public class MySelectTreeView extends LinearLayout  {
    private List<Structure> mDatas;//数据源
    private List<Structure> selectedDatas;
    private Context mContext;//上下文
    private LayoutInflater inflater;
    LayoutParams rootLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    public MySelectTreeView(Context mContext) {
        super(mContext);
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        selectedDatas = new ArrayList<>();
    }

    public void setDatas(List<Structure> mDatas) {
        this.mDatas = mDatas;
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView(){
        //添加一个线性布局作为一级菜单的容器
        LinearLayout rootLayout=new LinearLayout(mContext);
        rootLayoutParams.setMargins(30, 0, 0, 0);
        rootLayout.setLayoutParams(rootLayoutParams);
        rootLayout.setOrientation(LinearLayout.VERTICAL);

        //加载一级菜单
        //得到数据源中所有的一级菜单

        for (int i=0;i<mDatas.size();i++){
            final Structure firstBean = mDatas.get(i);
            //初始化一级布局，在这里只是简单的使用TextView作为布局
            View firstLevelView = inflater.inflate(R.layout.layout_first, null);
            //添加ID
            TextView name = (TextView) firstLevelView.findViewById(R.id.name);
            final ImageView rightIcon = (ImageView) firstLevelView.findViewById(R.id.right_icon);
            //设置名字
            name.setText(firstBean.getName());
            //添加一级菜单到一级菜单的容器中
            rootLayout.addView(firstLevelView);
            if(firstBean.getData() != null && firstBean.getData().size() > 0){
                //添加一个线性布局作为一级菜单的容器
                final LinearLayout firstLayout=getChildView(firstBean.getData());
                firstLayout.setVisibility(GONE);
                rootLayout.addView(firstLayout);
                firstLevelView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(firstLayout.getChildCount() > 0){
                            if(firstLayout.isShown()){
                                firstLayout.setVisibility(GONE);
                                rightIcon.animate().rotation(0);
                            }else{
                                firstLayout.setVisibility(View.VISIBLE);
                                rightIcon.animate().rotation(90);
                            }
                        }
                    }
                });
            }else{
                rightIcon.setImageResource(R.mipmap.blue_right);
                rightIcon.setVisibility(INVISIBLE);
                firstLevelView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rightIcon.isShown()){
                            rightIcon.setVisibility(INVISIBLE);
                            selectedDatas.remove(firstBean);
                        }else{
                            rightIcon.setVisibility(VISIBLE);
                            selectedDatas.add(firstBean);
                        }
                    }
                });
            }
        }
        addView(rootLayout);
    }

    private LinearLayout getChildView(final List<Structure> datas){
        //添加一个线性布局作为一级菜单的容器
        LinearLayout parentLayout=new LinearLayout(mContext);
        rootLayoutParams.setMargins(30, 0, 0, 0);
        parentLayout.setLayoutParams(rootLayoutParams);
        parentLayout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < datas.size(); i++) {
            final Structure structure = datas.get(i);
            if(structure.getData() != null && structure.getData().size() > 0){
                View childView = inflater.inflate(R.layout.layout_second, null);
                TextView childName = (TextView) childView.findViewById(R.id.childName);
                final ImageView img = (ImageView) childView.findViewById(R.id.img);
                childName.setText(structure.getName());
                parentLayout.addView(childView);
                //添加一个线性布局作为一级菜单的容器
                final LinearLayout childLayout=new LinearLayout(mContext);
                childLayout.setLayoutParams(rootLayoutParams);
                childLayout.setOrientation(LinearLayout.VERTICAL);
                childLayout.addView(getChildView(structure.getData()));
                childLayout.setVisibility(GONE);
                parentLayout.addView(childLayout);
                childView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(childLayout.getChildCount() > 0){
                            if(childLayout.isShown()){
                                childLayout.setVisibility(GONE);
                                img.animate().rotation(0);
                            }else{
                                childLayout.setVisibility(VISIBLE);
                                img.animate().rotation(90);
                            }
                        }
                    }
                });
            }else{
                View childView = inflater.inflate(R.layout.layout_third, null);
                TextView childName = (TextView) childView.findViewById(R.id.secondName);
                final ImageView checkbox = (ImageView) childView.findViewById(R.id.checkbox);
                childName.setText(structure.getName());
                parentLayout.addView(childView);
                childView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(selectedDatas.contains(structure)){
                            selectedDatas.remove(structure);
                            checkbox.setVisibility(INVISIBLE);
                        }else{
                            selectedDatas.add(structure);
                            checkbox.setVisibility(VISIBLE);
                        }
                    }
                });
            }
        }
        return parentLayout;
    }

    public List<Structure> getSelectedDatas() {
        return selectedDatas;
    }
}
